package pl.un4giv3n.mightvsmagic.utils.display;

import java.util.Locale;

import javax.servlet.jsp.PageContext;

import org.displaytag.decorator.TableDecorator;
import org.displaytag.model.TableModel;

import pl.un4giv3n.mightvsmagic.entity.ItemTemplate;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.TextProviderFactory;

public class DisplayItemTemplateDecorator extends TableDecorator implements
		LocaleProvider {

	private TextProvider resourceBundle;

	@Override
	public void init(PageContext pageContext, Object decorated,
			TableModel tableModel) {
		super.init(pageContext, decorated, tableModel);
		this.resourceBundle = new TextProviderFactory().createInstance(
				getClass(), this);

	}

	public String getPrice(){
		ItemTemplate  item = (ItemTemplate) getCurrentRowObject();
		if(item.getPrice() == 0){
			return item.getPremiumPrice() + " <img src=\"./img/icons/dragonteeth.jpg\" alt=\"Smocze zęby\" title=\"Smocze zęby\"/>";
		}else{
			return item.getPrice() + " <img src=\"./img/icons/gold.jpg\" alt=\"Złoto\" title=\"Złoto\"/>";
		}
	}

	public String getType(){
		ItemTemplate  item = (ItemTemplate) getCurrentRowObject();
		return resourceBundle.getText(item.getType().name());
	}
	
	public String getDmg(){
		ItemTemplate item = (ItemTemplate) getCurrentRowObject();
		return item.getMinDmg()+"-"+item.getMaxDmg();
	}
	
	public String getDef(){
		ItemTemplate item = (ItemTemplate) getCurrentRowObject();
		return item.getMinDef()+"-"+item.getMaxDef();
	}
	public String getModifieres() {
		ItemTemplate item = (ItemTemplate) getCurrentRowObject();
		StringBuilder builder = new StringBuilder();
		if(item.getStrenghtModifier() != 0){
			builder.append("Siła: ").append(item.getStrenghtModifier()).append("<br/>");
		}
		if(item.getStaminaModifier() != 0){
			builder.append("Wytrzymałośc: ").append(item.getStaminaModifier()).append("<br/>");
		}
		if(item.getManaModifier() != 0){
			builder.append("Mana: ").append(item.getManaModifier()).append("<br/>");
		}
		if(item.getAttackModifier() != 0){
			builder.append("Sztuka walki: ").append(item.getAttackModifier()).append("<br/>");
		}
		if(item.getParryModifier() != 0){
			builder.append("Unik: ").append(item.getParryModifier()).append("<br/>");
		}
		if(item.getSpeedModifier() != 0){
			builder.append("Szybkość: ").append(item.getSpeedModifier()).append("<br/>");
		}
		return builder.toString();
	}
	
	public String getImg(){
		ItemTemplate item = (ItemTemplate) getCurrentRowObject();
		StringBuilder builder = new StringBuilder();
		builder.append("<img src='").append(item.getImgUrl()).append("' />");
		return builder.toString();
	}

	public String getRequirements() {
		ItemTemplate item = (ItemTemplate) getCurrentRowObject();
		StringBuilder builder = new StringBuilder();
		if(item.getStrenghtReq() != 0){
			builder.append("Siła: ").append(item.getStrenghtReq()).append("<br/>");
		}
		if(item.getStaminaReq() != 0){
			builder.append("Wytrzymałośc: ").append(item.getStaminaReq()).append("<br/>");
		}
		if(item.getManaReq() != 0){
			builder.append("Mana: ").append(item.getManaReq()).append("<br/>");
		}
		if(item.getAttackReq() != 0){
			builder.append("Sztuka walki: ").append(item.getAttackReq()).append("<br/>");
		}
		if(item.getParryReq() != 0){
			builder.append("Unik: ").append(item.getParryReq()).append("<br/>");
		}
		if(item.getRequiredAnyExp() > 0){
			builder.append("Doświadczenie: ").append(item.getRequiredAnyExp()).append("<br/>");
		}else{
			if(item.getRequiredFightExp() != 0){
				builder.append("Doświadczenie bojowe: ").append(item.getRequiredFightExp()).append("<br/>");
			}
			if(item.getRequiredMagicExp() != 0){
				builder.append("Doświadczenie magiczne: ").append(item.getRequiredMagicExp()).append("<br/>");
			}
		}
		return builder.toString();
	}

	@Override
	public Locale getLocale() {
		ActionContext ctx = ActionContext.getContext();
		if (ctx != null) {
			return ctx.getLocale();
		} else {
			return Locale.getDefault();
		}
	}
}
